﻿using System;
using System.ComponentModel;
using Mbs.Globalization;

namespace Mbs.ComponentModel
{
    [Serializable]
    public class LocalizedObject : DisposableObject, ICustomTypeDescriptor,Globalization.ISupportGlobalization
    {
        protected LocalizedObject()
        {
            SupportGlobalization.IsSupportGlobalization = true;
            LanguageManager.Instance.Register(SupportGlobalization);
        }

        private ISupportGlobalization SupportGlobalization
        {
            get { return this as ISupportGlobalization; }
        }

        PropertyDescriptorCollection globalizedProps;

        string ICustomTypeDescriptor.GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        AttributeCollection ICustomTypeDescriptor.GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        string ICustomTypeDescriptor.GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        TypeConverter ICustomTypeDescriptor.GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        /// <summary>
        /// Causes the list of property descriptors to be recreated.
        /// </summary>
        protected void ReFilterProperties()
        {
            globalizedProps = null;
        }

        protected virtual void FilterProperties(PropertyDescriptorCollection globalizedProps)
        {
        }

        /// <summary>
        /// Called to get the properties of a type.
        /// </summary>
        /// <param name="attributes"></param>
        /// <returns></returns>
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            if (globalizedProps == null)
            {
                // Get the collection of properties
                PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this, attributes, true);

                globalizedProps = new PropertyDescriptorCollection(null);

                // For each property use a property descriptor of our own that is able to be globalized
                foreach (PropertyDescriptor oProp in baseProps)
                {
                    globalizedProps.Add(new LocalizedPropertyDescriptor(oProp));
                }
                FilterProperties(globalizedProps);
            }
            return globalizedProps;
        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
        {
            // Only do once
            if (globalizedProps == null)
            {
                // Get the collection of properties
                PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this, true);
                globalizedProps = new PropertyDescriptorCollection(null);

                // For each property use a property descriptor of our own that is able to be globalized
                foreach (PropertyDescriptor oProp in baseProps)
                    globalizedProps.Add(new LocalizedPropertyDescriptor(oProp));
                FilterProperties(globalizedProps);
            }
            return globalizedProps;
        }

        
        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        public virtual void InformSetValue(PropertyDescriptor propertyDescriptor, object component, object value)
        {

        }

        [Browsable(false)]
        bool Mbs.Globalization.ISupportGlobalization.IsSupportGlobalization { get; set; }

        public virtual void InitializeResource()
        {
           
        }

        void Mbs.Globalization.ILanguageChangedListner.RefreshResource()
        {
            InitializeResource();
        }

        protected override void Dispose(bool disposing)
        {
            LanguageManager.Instance.UnRegister(this);
            base.Dispose(disposing);
        }

    }
}